from ProjectTool.WebTool.base_func.base_func import BaseFunc
from selenium.webdriver.common.by import By
import time


# 添加工具
class EditTool(BaseFunc):
    def __init__(self,http_status,host,port,domain,local_storage,session_storage, driver=None,driver_status=None):
        super().__init__(http_status,host,port,domain,local_storage,session_storage,driver=driver,driver_status=driver_status)
        self.page_url = 'AddOftenMyTool'
        # self.run()
        self.table_fields = self._get_table_fields()  # 获取表格字段
        self._get_page_elements()  # 获取标签地址
        self.page_data = {}  # 页面中需要验证的数据

    # 表格字段 及数据类型
    def _get_table_fields(self):
        return [('id', int), ('title', str), ('URL', str), ('remark', str)]

    # 标签地址存放区
    def _get_page_elements(self):
        self.input_title = (By.XPATH, '//label[contains(text(),"{title}")]/following-sibling::div[1]//input'.format(title='工具名称'))  # 工具名称
        self.input_url = (By.XPATH, '//label[contains(text(),"{title}")]/following-sibling::div[1]//input'.format(title='URL'))  # URL
        self.input_remark = (By.XPATH, '//label[contains(text(),"{title}")]/following-sibling::div[1]//input'.format(title='备注'))  # 备注

        self.button_save = (By.XPATH, '//button/span[contains(text(),"保存")]')  # 保存
        self.button_reset = (By.XPATH, '//button/span[contains(text(),"重置")]')  # 重置

    # 页面中需要验证的数据
    def _get_page_data(self):
        return self.page_data

    # 输入标题
    def send_title(self,*args, **kwargs):
        send_field = 'title'
        title = kwargs.get(send_field)
        element_input = self.find_element(self.input_title)
        self.send_keys(element_input, title)
        self.page_data[send_field] = title
        return self._get_page_data()

    # 输入url
    def send_url(self,*args, **kwargs):
        send_field = 'url'
        url = kwargs.get(send_field)
        element_input = self.find_element(self.input_url)
        self.send_keys(element_input, url)
        self.page_data[send_field] = url
        return self._get_page_data()

    # 输入备注
    def send_remark(self,*args, **kwargs):
        send_field = 'remark'
        remark = kwargs.get(send_field)
        element_input = self.find_element(self.input_remark)
        self.send_keys(element_input, remark)
        self.page_data[send_field] = remark
        return self._get_page_data()

    # 点击保存
    def click_save_form(self,*args, **kwargs):
        element_search = self.find_element(self.button_save)
        self.click(element_search)
        return self._get_page_data()

    # 点击重置
    def click_reset_form(self,*args, **kwargs):
        element_search = self.find_element(self.button_reset)
        self.click(element_search)
        return self._get_page_data()


if __name__ == '__main__':
    obj = EditTool()
    obj.send_title('测试-添加一个小工具')
    obj.send_url('http://www.baidu.com')
    obj.send_remark('备注哈哈哈哈哈啊沙发沙发沙发沙发沙发的')
    obj.click_reset_form()
    obj.send_title('测试-添加一个小工具11')
    obj.send_url('http://www.baidu.com')
    obj.send_remark('备注哈哈哈哈哈啊沙发沙发沙发沙发沙发的111')
    obj.click_save_form()
    time.sleep(2)
    obj.quit()
